﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace Feniks
{
    public partial class frmIsEmri : DevExpress.XtraEditors.XtraForm
    {
        public frmIsEmri()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            using (DataEntities ent = new DataEntities())
            {
                var sonuc = ent.IsEmriTanimlari.Where(x => x.IsEmriNo == cmbNo.Text).FirstOrDefault();
                if (sonuc != null)
                {
                    sonuc.FirmaAdi = txtFirma.Text;
                }
                else
                {
                    ent.AddToIsEmriTanimlari(new IsEmriTanim()
                    {
                        FirmaAdi = txtFirma.Text,
                        IsEmriNo = cmbNo.Text
                    });
                }
                ent.SaveChanges();
                txtFirma.Text = "";
                cmbNo.SelectedIndex = -1;
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            using (DataEntities ent = new DataEntities())
            {
                var sonuc = ent.IsEmriTanimlari.Where(x => x.IsEmriNo == cmbNo.Text).FirstOrDefault();
                if (sonuc != null)
                {
                    ent.DeleteObject(sonuc);
                    ent.SaveChanges();
                }
                txtFirma.Text = "";
                cmbNo.SelectedIndex = -1;
            }
        }

        private void frmIsEmri_Load(object sender, EventArgs e)
        {
            using (DataEntities ent = new DataEntities())
            {
                var sonuc = ent.IsEmriTanimlari.ToList();
                foreach (var item in sonuc)
                {
                    cmbNo.Properties.Items.Add(new ComboItem()
                    {
                        Text = item.IsEmriNo,
                        Value = item
                    });
                }
            }
        }

        private void cmbNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNo.SelectedIndex > -1)
            {
                IsEmriTanim item = (IsEmriTanim)((ComboItem)cmbNo.SelectedItem).Value;
                if (item != null)
                {
                    cmbNo.Text = item.IsEmriNo;
                    txtFirma.Text = item.FirmaAdi;
                }
            }
        }
    }
}